/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.activiti.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tompai.activiti.domain.SalaryDO;
import com.tompai.activiti.service.SalaryService;
import com.tompai.activiti.utils.ActivitiUtils;
import com.tompai.common.config.Constant;
import com.tompai.common.controller.BaseController;
import com.tompai.common.utils.MsgResult;
import com.tompai.common.utils.PageUtils;
import com.tompai.common.utils.Query;
import com.tompai.common.utils.ShiroUtils;

/**
 * @author: tompai
 * @email：liinux@qq.com
 * @createTime: 2019年11月24日 下午11:55:29
 * @history:
 * @version: v1.0
 */

@Controller
@RequestMapping("/act/salary")
public class SalaryController extends BaseController {
	@Autowired
	private SalaryService salaryService;
	@Autowired
	ActivitiUtils activitiUtils;

	@GetMapping()
	String Salary() {
		return "activiti/salary/salary";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		List<SalaryDO> salaryList = salaryService.list(query);
		int total = salaryService.count(query);
		PageUtils pageUtils = new PageUtils(salaryList, total);
		return pageUtils;
	}

	@GetMapping("/form")
	String add() {
		return "act/salary/add";
	}

	@GetMapping("/form/{taskId}")
	String edit(@PathVariable("taskId") String taskId, Model model) {
		SalaryDO salary = salaryService.get(activitiUtils.getBusinessKeyByTaskId(taskId));
		salary.setTaskId(taskId);
		model.addAttribute("salary", salary);
		return "act/salary/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public MsgResult saveOrUpdate(SalaryDO salary) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return MsgResult.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		salary.setCreateDate(new Date());
		salary.setUpdateDate(new Date());
		salary.setCreateBy(ShiroUtils.getUserId().toString());
		salary.setUpdateBy(ShiroUtils.getUserId().toString());
		salary.setDelFlag("1");
		if (salaryService.save(salary) > 0) {
			return MsgResult.ok();
		}
		return MsgResult.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public MsgResult update(SalaryDO salary) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return MsgResult.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		String taskKey = activitiUtils.getTaskByTaskId(salary.getTaskId()).getTaskDefinitionKey();
		if ("audit2".equals(taskKey)) {
			salary.setHrText(salary.getTaskComment());
		} else if ("audit3".equals(taskKey)) {
			salary.setLeadText(salary.getTaskComment());
		} else if ("audit4".equals(taskKey)) {
			salary.setMainLeadText(salary.getTaskComment());
		} else if ("apply_end".equals(salary.getTaskComment())) {
			// 流程完成，兑现
		}
		salaryService.update(salary);
		return MsgResult.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public MsgResult remove(String id) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return MsgResult.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if (salaryService.remove(id) > 0) {
			return MsgResult.ok();
		}
		return MsgResult.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	public MsgResult remove(@RequestParam("ids[]") String[] ids) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return MsgResult.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		salaryService.batchRemove(ids);
		return MsgResult.ok();
	}

}
